<template>
<div>
  <div class="table-data">
    <div class="search-box">
      <el-input size="small" v-model="searchMap.name" placeholder="供应商名称"></el-input>
      <el-button size="small" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button size="small" type="primary" @click="handleAdd()"><i class="el-icon-plus"></i><span>添加供应商</span></el-button>
    </div>
    <el-table 
      v-loading="loading"
      element-loading-text="正在加载"
      element-loading-spinner="el-icon-loading" :data="tableData" :header-cell-style="headClass" style="width:100%" :height="tHeight" class="table-box">
      <el-table-column label="ID" prop="id" width="100"></el-table-column>
      <el-table-column label="供应商" prop="name"></el-table-column>
      <el-table-column label="用户名" prop="username"></el-table-column>
      <!-- <el-table-column label="地址" prop="address"></el-table-column> -->
      <el-table-column label="电话" prop="phone"></el-table-column>
      <!-- <el-table-column label="详细地址" prop="desc"></el-table-column> -->
      <el-table-column label="创建时间" prop="createtime"></el-table-column>
      <el-table-column label="更新时间" prop="updatetime"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <router-link :to="{name:'goodsList',query:{vend_id:scope.row.id}}" @click.native="handleGoods(scope.row.id)" class="user-detail el-button el-button--primary el-button--mini">查看商品</router-link>
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pages" ref="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10,30,50]"
        :page-size="size"
        layout="total,sizes,prev,pager,next,jumper"
        :total="total"
      ></el-pagination>

    </div>
    <!-- <AddDialog :isAdd="isAdd" @closeDialog="closeDialog" @loadData="loadData"></AddDialog> -->
    <EditDialog :isEdit="isEdit" :isAdd="isAdd" :key="timer" :form="formData" @closeDialog="closeDialog" @loadData="loadData"></EditDialog>
  </div>
  </div>
</template>

<script lang="ts">

import { Component, Vue, Provide,Prop,Watch } from "vue-property-decorator";
// import { State, Getter, Mutation, Action } from "vuex-class";

import EditDialog from "./EditDialog.vue";
import supplierApi from "@/api/supplier";
import commonApi from "@/api/common";
import bus from "@/api/busEvevt"

@Component({
  components: {EditDialog }
})
export default class supplierList extends Vue {
  // @Getter("user") getUser: any;
  @Provide() dateTime: any = ""; // 时间
  @Provide() tHeight: number = document.body.offsetHeight - 323;
  @Provide() tableData: any = []; // 表格数据
  @Provide() multipleTable: any = []; // 表格数据
  @Provide() page: number = 1; // 当前page
  @Provide() size: number = 10; // 请求数据的个数 默认5
  @Provide() total: number = 0; // 总数据条数
  @Provide() isAdd: Boolean = false; // 是否展示添加弹窗
  @Provide() isEdit: Boolean = false; // 是否展示编辑弹窗
  @Provide() advertisers: any = [];
  @Provide() regionList:any=[] //地址
  @Provide() timer: any = '';
  @Provide() loading: Boolean = true; // 是否显示加载图标
  @Provide() formData: any = {
    id:"",
    name: "",
    username: "",
    phone:"",
  };
  @Provide() searchMap:{
    name: string,
    phone:string,
  }={
    name:'',
    phone:''
  }
  handleGoods(id){
    bus.$emit('vendId',id)
  }
  headClass() {
            return "text-align: center;background:#eef1f6;";
        }
 // 新增
  handleAdd(){
    this.timer = new Date().getTime();
    this.isAdd=true;
    this.isEdit=true;
    this.formData={id:"",name: "",username:"",phone:""}
  }
  // 编辑
  handleEdit(index: number, row: any){
    this.timer = new Date().getTime();
    this.isEdit=true;
    this.formData=row
  }
  // 关闭弹窗
  closeDialog() {
    this.isAdd = false;
    this.isEdit = false;
    this.loadData();
  }

  created() {
    this.loadData();
  }

  loadData() {
    supplierApi.supplierList(this.page,this.size,this.searchMap)
      .then((res: any) => {
        this.tableData = res.data.data.lists.data;
        this.total = res.data.data.lists.total;
        this.loading=false
    });
  }

  handleSizeChange(val: number): void {
    this.size = val;
    this.page = 1;
    this.loadData();
  }

  handleCurrentChange(val: number): void {
    this.page = val;
    this.loadData();
  }

  handleSearch(): void {
    // 点击搜索
    this.page = 1;
    this.loadData();
  }
}
</script>

<style lang="scss">
.el-range-editor.el-input__inner{
  margin-right: 10px
}
</style>
